//Example program from qt.nokia.com

#include <QtGui>
#include "mainwindow.h"
//#include "netcomms.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Notepad mainWindow;
    mainWindow.show();

    return app.exec();
}

Notepad::Notepad()
   {
       openAction = new QAction(tr("&Open"), this);
       saveAction = new QAction(tr("&Save"), this);
       exitAction = new QAction(tr("E&xit"), this);

       connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
       connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
       connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

       fileMenu = menuBar()->addMenu(tr("&File"));
       fileMenu->addAction(openAction);
       fileMenu->addAction(saveAction);
       fileMenu->addSeparator();
       fileMenu->addAction(exitAction);

       textEdit = new QTextEdit;
       setCentralWidget(textEdit);

       setWindowTitle(tr("Notepad"));
   }

void Notepad::open()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "",
          tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));

      if (fileName != "") {
          QFile file(fileName);
          if (!file.open(QIODevice::ReadOnly)) {
              QMessageBox::critical(this, tr("Error"),
                  tr("Could not open file"));
              return;
          }
          QString contents = file.readAll().constData();
          textEdit->setPlainText(contents);
          file.close();
      }
}

void Notepad::save()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "",
        tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));

    if (fileName != "") {
        QFile file(fileName);
        if (!file.open(QIODevice::WriteOnly)) {
            // error message
        } else {
            QTextStream stream(&file);
            stream << textEdit->toPlainText();
            stream.flush();
            file.close();
        }
    }
}
